﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Employees_EmpInsurance : System.Web.UI.Page
{
    private Srvtools.WebDataSet WMaster;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            if (Request["action"] != null)
            {
                InitializeComponent(true);
                WMaster.SetWhere("EmpInsurance.EmployeeId=" + Request["eid"].ToString());
                wgvMaster.Visible = true;
            }
            else
            {
                wgvType.DataBind();

                if (Request["IsSearch"] != null)
                {
                    if (Request["IsSearch"].ToString() == "1")
                    {
                        SBind();
                    }

                }
                else
                {
                    InitializeComponent(false);
                    wgvMaster.Visible = false;
                }
            }


            Master.DataSource = WMaster;



            ((HiddenField)this.Page.Master.FindControl("HoverId")).Value = "EmpInsurance";



            MasterPage_MasterPage_EmpDetail master = (MasterPage_MasterPage_EmpDetail)this.Page.Master;

            master.HelpHref = "";
            master.PageName = "员工保险";
            master.PicUrl = "~/image/PageIcon/hetong.jpg";
            master.IntiBar();
        }
    }

    private void InitializeComponent(bool action)
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Employees_EmpInsurance));
        this.WMaster = new Srvtools.WebDataSet();
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).BeginInit();
        // 
        // WMaster
        // 
        this.WMaster.Active = action;
        this.WMaster.AlwaysClose = false;
        this.WMaster.PacketRecords = -1;
        this.WMaster.Position = -1;
        this.WMaster.RemoteName = "SEmployees.ViewEmpInsurance";
        this.WMaster.ServerModify = false;
        ((System.ComponentModel.ISupportInitialize)(this.WMaster)).EndInit();
    }

    protected void Search_Click(object sender, EventArgs e)
    {

        string url = "IsSearch=1";

        string strDep = Depart.SelectDepmanetId;
        if (strDep != "0" && strDep != "")
        {
            url += "&dep=" + strDep;
        }
        else
        {
            url += "&dep=";
        }


        string sname = SelUser.EmpName;
        if (sname != "")
        {
            url += "&name=" + Server.UrlEncode(sname);
        }
        else
        {
            url += "&name=";
        }

        url += "&st=" + Accession.Text;
        url += "&et=" + Dimission.Text;


        url += "&tmode=" + ddlTimeOption.SelectedValue;
   

        switch (overdue.SelectedValue)
        {
            case "-1":
                url += "&ddl=-1";

                break;
            case "0":
                url += "&ddl=0";
                break;
            case "1":
                url += "&ddl=1";
                break;
            case "2":
                url += "&ddl=2";
                break;
            case "3":
                url += "&ddl=3";
                break;
        }


        url += "&insClass=" + wgvType.SelectedValue;


        Response.Redirect("EmpInsurance.aspx?" + url);

    }

    private void SBind()
    {
        
        Search.CssClass = "OnHover";
        InitializeComponent(true);


        string strDep = Request["dep"].ToString();
        string sname = Server.UrlDecode(Request["name"]);
        string over = Request["ddl"].ToString();
        string insClass = Request["insClass"].ToString();
        string so = Request["tmode"].ToString();
        string bt = Request["st"].ToString();
        string et = Request["et"].ToString();

        ddlTimeOption.SelectedValue = so;
        if (so == "0")
        {
            if (bt != "")
            {
                Accession.Text = bt;
                bt = " And EmpInsurance.StartDate > '" + bt + " 00:00:00'";
            }
            else
            {
                bt = "";
            }

            if (et != "")
            {
                Dimission.Text = et;
                et = " And EmpInsurance.StartDate < '" + et + " 23:59:59'";
            }
            else
            {
                et = "";
            }
        }
        else
        {
            if (bt != "")
            {
                Accession.Text = bt;
                bt = " And EmpInsurance.EndDate > '" + bt + " 00:00:00'";
            }
            else
            {
                bt = "";
            }

            if (et != "")
            {
                Dimission.Text = et;
                et = " And EmpInsurance.EndDate < '" + et + " 23:59:59'";
            }
            else
            {
                et = "";
            }
        }
        
        if (strDep != "0" && strDep != "")
        {
            Depart.IsAfterBind = strDep;
            strDep = " Employees.DepartmentId like ('" + strDep + "%')";

        }
        else
        {
            strDep = " Employees.DepartmentId <> -1";
        }



        if (sname != "")
        {
            SelUser.EmpName = sname;
            sname = " And EmpName like ('%" + sname + "%')";
        }
        else
        {

            sname = "";
        }

        if (insClass != "-1")
        {
            wgvType.SelectedValue = insClass;
            insClass = " And CommInsurance.InsuranceClass = " + insClass;
        }
        else
        {
            wgvType.SelectedValue = "-1";
            insClass = "";
        }

        switch (over)
        {
            case "-1":
                overdue.SelectedValue = "-1";
                over = "";
                break;
            case "0":
                overdue.SelectedValue = "0";
                over = " And DATEDIFF(day, EmpInsurance.EndDate, getdate()) < 0 and EmpInsurance.Overdue=0 and Employees.Empstatus=1";
                break;
            case "1":
                overdue.SelectedValue = "1";
                over = " And Overdue=1  and Employees.Empstatus=1";
                break;
            case "2":
                overdue.SelectedValue = "2";
                over = " And DATEDIFF(day, EmpInsurance.EndDate, getdate()) >= 0 and EmpInsurance.Overdue=0 and Employees.Empstatus=1";
                break;
            case "3":
                overdue.SelectedValue = "3";
                over = " And DATEDIFF(day, EmpInsurance.EndDate, getdate()) >= 0 and EmpInsurance.Overdue=1 and Employees.Empstatus=1";
                break;
        }




        WMaster.SetWhere(strDep + sname + over + insClass + bt + et);
        Master.DataSource = WMaster;

        wgvMaster.Visible = true;
    }

    protected void wgvMaster_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;

            ImageButton imb = (ImageButton)e.Row.FindControl("imb");

            //if (drv["ContractId"].ToString() == "")
            //{
            if (Request["action"] != null)
            {
                imb.Attributes.Add("onclick", "location.href='EmpInsuranceDetail.aspx?Iid=" + drv["InsuranceDataId"].ToString() +
                   "&action=" + Request["action"].ToString() + "&eid=" + drv["EmployeeId"].ToString() + "&ename=" + Server.UrlEncode(drv["EmpName"].ToString()) + "&type=" + drv["TypeId"].ToString() + "&redurl=" + Server.UrlEncode(Request.Url.ToString()) + "';return false;");
            }
            else
            {
                imb.Attributes.Add("onclick", "location.href='EmpInsuranceDetail.aspx?Iid=" + drv["InsuranceDataId"].ToString() +
                "&action=edit&type=" + drv["TypeId"].ToString() + "&eid=" + drv["EmployeeId"].ToString() + "&ename=" + Server.UrlEncode(drv["EmpName"].ToString()) + "&redurl=" + Server.UrlEncode(Request.Url.ToString()) + "';return false;");

            }
            string ed = drv["EndDate"].ToString();

            if ((bool)drv["overdue"])
            {
                if (ed == "")
                {
                    ((Label)e.Row.FindControl("overdue")).Text = "<font style='color:silver'>已作废 | 投保日期空</font>";
                }
                else if (Convert.ToDateTime(ed) <= DateTime.Now)
                {
                    ((Label)e.Row.FindControl("overdue")).Text = "<font style='color:silver'>已作废 | 投保时间逾期</font>";
                }
                else
                {
                    ((Label)e.Row.FindControl("overdue")).Text = "<font style='color:silver'>已作废</font>";
                }
            }
            else
            {
                if (ed == "")
                {
                    ((Label)e.Row.FindControl("overdue")).Text = "<b>投保日期空</b>";
                }
                else if (Convert.ToDateTime(ed) <= DateTime.Now)
                {
                    ((Label)e.Row.FindControl("overdue")).Text = "<b><span style='color:red'>投保时间逾期</span></b>";
                }
                else
                {
                    ((Label)e.Row.FindControl("overdue")).Text = "<b>正常</b>";
                }
            }


        }
    }


    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Response.Redirect("EmpInsuranceDetail.aspx?action=add&redurl=" + Server.UrlEncode(Request.Url.ToString()));
    }
    protected void wgvType_DataBound(object sender, EventArgs e)
    {
        wgvType.Items.Add(new ListItem("未选择", "-1"));
        wgvType.SelectedValue = "-1";
    }

    protected void wgvMaster_DataBound(object sender, EventArgs e)
    {
        if (Request["action"] != null)
        {

            SearchForm.Visible = false;
            if (Request["action"].ToString() == "edit")
            {
                TabsFunction.Visible = true;

                string eid = Request["eid"].ToString();
                object[] oj = Srvtools.CliUtils.CallMethod("SEmployees", "GetEmpNameByEid", new object[] { eid });
                string ename = oj[1].ToString();
                AddFunction.Attributes.Add("onclick", "ymPrompt.win({title:'保 险 添 加',message:'EmpInsuranceDetail.aspx?eid=" + eid + "&ename=" + Server.UrlEncode(ename)
               + "&action=onlyadd',closeBtn:true,iframe:true,allowRightMenu:false,allowSelect:false});ymPrompt.max();return false");
            }
            else
            {
                TabsFunction.Visible = false;
            }
        }
        else
        {
            TabsFunction.Visible = false;
        }
    }


    protected void DownLoad_Click(object sender, EventArgs e)
    {
        try
        {
            object obj = Master.InnerDataSet;

            string path = MapPath("~/UploadFile/Template/");

            DataTable dt = ((DataSet)obj).Tables[0];


            if (dt.Rows.Count <= 0)
            {
                ClientScript.RegisterStartupScript(GetType(), "Msg", "alert('没有记录')", true);
                return;
            }
            DataTable dts = GetDataTable();

            foreach (DataRow dr in dt.Rows)
            {
                DataRow SR = dts.NewRow();
                SR["保险编号"] = dr["InsuranceDataId"];
                SR["员工编号"] = dr["EmployeeID"];
                SR["员工姓名"] = dr["EmpName"];

                SR["部门"] = dr["DepartmentName"];

                SR["投保开始"] = dr["StartDate"];
                SR["投保结束"] = dr["EndDate"];

                string ed = dr["EndDate"].ToString();
                if ((bool)dr["overdue"])
                {
                    if (ed == "")
                    {
                        SR["状态"] = "已作废 | 投保日期空";
                    }
                    else if (Convert.ToDateTime(ed) <= DateTime.Now)
                    {
                        SR["状态"] = "已作废 | 投保时间逾期";
                    }
                    else
                    {
                        SR["状态"] = "已作废";
                    }
                }
                else
                {
                    if (ed == "")
                    {
                        SR["状态"] = "投保日期空";
                    }
                    else if (Convert.ToDateTime(ed) <= DateTime.Now)
                    {
                        SR["状态"] = "投保时间逾期";
                    }
                    else
                    {
                        SR["状态"] = "正常";
                    }
                }
                
                dts.Rows.Add(SR);
            }


            DataTable dtt = dts.Clone();
            dtt.Merge(dts);
            Srvtools.InfoDataSet ids = new Srvtools.InfoDataSet();
            ids.RemoteName = "GLModule.cmdColDEF";
            ids.RealDataSet.Tables.Add(dtt);

            ids.ToExcel(0, path + "empins.xls");


            System.IO.FileInfo fn = new System.IO.FileInfo(path + "empins.xls");

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ContentType = "application/octet-stream";
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fn.Name);
            HttpContext.Current.Response.WriteFile(fn.FullName);
            HttpContext.Current.Response.End();
        }
        catch
        {

        }
    }

    private DataTable GetDataTable()
    {
        DataTable WTable = new DataTable("DownLoadDT");
        {
            WTable.Columns.Add(new DataColumn("保险编号", typeof(Int32)));
            WTable.Columns.Add(new DataColumn("员工编号", typeof(Int32)));
            WTable.Columns.Add(new DataColumn("员工姓名", typeof(string)));
            WTable.Columns.Add(new DataColumn("部门", typeof(string)));
            WTable.Columns.Add(new DataColumn("投保开始", typeof(string)));
            WTable.Columns.Add(new DataColumn("投保结束", typeof(string)));
            WTable.Columns.Add(new DataColumn("状态", typeof(string)));
        }
        return WTable;
    }
}
